﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using WPF_UI_Lightningchart.Models;

namespace WPF_UI_Lightningchart.Services
{
    public class LayoutManager
    {
        private readonly string _layoutFolderPath = "Layouts";

        public LayoutManager()
        {
            if (!Directory.Exists(_layoutFolderPath))
                Directory.CreateDirectory(_layoutFolderPath);
        }

        // 保存布局到文件
        public void SaveLayout(string layoutName, LayoutData layoutData)
        {
            var json = JsonConvert.SerializeObject(layoutData, Formatting.Indented);
            File.WriteAllText(Path.Combine(_layoutFolderPath, $"{layoutName}.json"), json);
        }

        // 从文件加载布局
        public LayoutData LoadLayout(string layoutName)
        {
            var path = Path.Combine(_layoutFolderPath, $"{layoutName}.json");
            if (!File.Exists(path)) return null;

            var json = File.ReadAllText(path);
            var layoutData = JsonConvert.DeserializeObject<LayoutData>(json);
            return layoutData;
        }

        // 获取现有的布局文件列表
        public List<string> GetSavedLayouts()
        {
            var files = Directory.GetFiles(_layoutFolderPath, "*.json");
            return files.Select(file => Path.GetFileNameWithoutExtension(file)).ToList();
        }
    }
}
